﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Scripting.Hosting;
using System.Reflection;
using System.IO;
using Flame.Reflection;

namespace Flame.Dlr
{
    public class IronPythonExec : AExecutablePR
    {
        protected override ScriptEngine ScriptEngine { get { return IronPython.Hosting.Python.CreateEngine(); } }
        protected override Result checkForSpecialType(Result cr) { return cr; }

        protected override void clearScope(ScopeParameters scopeParameters)
        {
            Execute("import sys");
            Execute("import clr");
        }

        public override ResultAssembly AddAssembly(AssemblyWrapper assembly)
        {
            FileInfo f = new FileInfo(assembly.Location);
            Execute("sys.path.append(r\"" + f.Directory.FullName + "\")");
            Execute("clr.AddReferenceToFile(\"" + f.Name + "\")");
            try
            {
                Execute("import " + f.Name.Substring(0, f.Name.Length - f.Extension.Length));
            }
            catch { }
            return new ResultAssembly() { Exception = null, Loaded = true, Language = GetType() };
        }
      

        PythonIntellisense _intellisense = null;
        public override IIntellisense Intellisense
        {
            get
            {
                if (_intellisense == null) _intellisense = new PythonIntellisense(this); 
                return _intellisense; 
            }
        }
    }
}
